package com.example.zhc36.fragment.apadter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.zhc36.fragment.R;
import com.example.zhc36.fragment.bean.Featurebean;
import com.example.zhc36.fragment.dao.FeatureDao;

import java.util.List;

public class Rvnewmoreadapter extends RecyclerView.Adapter<Rvnewmoreadapter.BaseViewHolder> {
    private final List<Featurebean> listopen;
    private final List<Featurebean> listclose;
    private FeatureDao dao;

    public Rvnewmoreadapter(List<Featurebean> listopen, List<Featurebean> listclose) {
        this.listopen = listopen;
        this.listclose = listclose;
        dao = new FeatureDao();
    }

    private final int TYPE_HEAD = 1;
    private final int TYPE_BEAN = 2;

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else if (position < 1 + listclose.size()) {
            return TYPE_BEAN;
        } else if (position == 1 + listclose.size()) {
            return TYPE_HEAD;
        } else {
            return TYPE_BEAN;
        }
    }

    @Override
    public int getItemCount() {
        return listopen.size() + listclose.size() + 2;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        BaseViewHolder vh = null;
        switch (viewType) {
            case TYPE_HEAD:
                View viewed = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.item_more_news_hand, viewGroup, false);
                vh = new HeadViewHolder(viewed);
                break;
            case TYPE_BEAN:
                View view = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.item_more_news, viewGroup, false);
                vh = new BeanViewHolder(view);
                break;
        }
        return vh;
    }


    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder viewHolder, int i) {
        if (viewHolder instanceof BeanViewHolder) {

            BeanViewHolder vh = (BeanViewHolder) viewHolder;
            if (i < 1 + listclose.size()) {
                Featurebean featurebean1 = listclose.get(i - 1);
                vh.setData(featurebean1);
            } else {
                Featurebean featurebean2 = listopen.get(i - listclose.size() - 2);
                vh.setData(featurebean2);
            }
        } else if (viewHolder instanceof HeadViewHolder) {
            HeadViewHolder vh = (HeadViewHolder) viewHolder;
            vh.setData(i);
        }
    }

    public static class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }

    }


    public class HeadViewHolder extends BaseViewHolder {
        private TextView tv;

        public HeadViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }

        public void setData(int ii) {
            tv.setText(ii == 0 ? "已开启的功能" : "已关闭的功能");

        }
    }


    public class BeanViewHolder extends BaseViewHolder implements View.OnClickListener {
        private Button bt1;
        private TextView tv;
        private Featurebean bean;

        public BeanViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            bt1 = itemView.findViewById(R.id.bt1);
            bt1.setOnClickListener(this);


        }

        public void setData(Featurebean bean) {
            this.bean = bean;
            tv.setText(bean.getName());
            if ("0".equals(bean.getShowType())) {
                bt1.setText("开启");
            } else {
                bt1.setText("关闭");
            }
        }

        @Override
        public void onClick(View view) {


            if (view == bt1) {
//                //删除数据库
//                bean.setShowType("1".equals(bean.getShowType()) ? "0" : "1");
//                dao.Update(bean);
//
//                //清除数据listopen，listclose
//                listclose.clear();
//                listopen.clear();
//                //重新查数据，把数据放入listopen，listclose
//
//                listclose.addAll(dao.findshow());
//                listopen.addAll(dao.findAllClose());
//                //刷新界面
//                notifyDataSetChanged();


                //删除数据库

                //清除数据listopen，listclose
                int from;
                int to;
                if ("1".equals(bean.getShowType())) {
                    int i = listclose.indexOf(bean);
                    listclose.remove(bean);
                    from = i + 1;
                    listopen.add(0, bean);
                    to = listclose.size() + 2;
                } else {
                    int i = listopen.indexOf(bean);
                    listopen.remove(bean);
                    from = i + listclose.size() + 2;
                    listclose.add(0, bean);
                    to = 1;
                }

                //修改数据库
                bean.setShowType("0".equals(bean.getShowType()) ? "1" : "0");
                dao.Update(bean);
                //刷新界面

                notifyItemMoved(from, to);
                notifyItemChanged(to);
            }
        }
    }
}



